from django.http import JsonResponse
from web.models import PlatformInfo
from django import forms
from django.forms.models import model_to_dict


"""

错误码:
0: 成功
1: 失败
2: 参数错误
3: 请求方式错误

"""

# 平台信息表单
class PlatformInfoForm(forms.ModelForm):
    class Meta:
        model = PlatformInfo
        fields = [
            'name',
            'logo',
            'description',
        ]
    
def model_to_dict_with_url(model):
    model_dict = model_to_dict(model)
    if model_dict['logo']:
        model_dict['logo'] = model.logo.url
    else:
        model_dict['logo'] = ""
    model_dict['create_time'] = model.create_time.strftime('%Y-%m-%d %H:%M:%S')
    model_dict['update_time'] = model.update_time.strftime('%Y-%m-%d %H:%M:%S')
    return model_dict


# 创建平台信息
def create_platform_info(request):
    if request.method == 'POST':
        form = PlatformInfoForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            # 返回平台信息
            platform_info = model_to_dict_with_url(form.instance)
            return JsonResponse({
                'code': 0,
                'message': '平台信息创建成功',
                'data': platform_info,
            })
        else:
            return JsonResponse({
                'code': 1,
                'message': '平台信息创建失败',
                'data': form.errors,
            })
    else:
        return JsonResponse({
            'code': 3,
            'message': '请求方式错误',
        })

# 获取平台信息
def get_platform_info(request):
    if request.method == 'POST':
        PlatformInfoID = request.POST.get('PlatformInfoID')
        if not PlatformInfoID:
            return JsonResponse({
                'code': 2,
                'message': '平台信息ID不能为空',
            })
        try:
            platform_info = PlatformInfo.objects.filter(platform_id=PlatformInfoID).first()
            if platform_info:
                return JsonResponse({
                    'code': 0,
                    'message': '获取平台信息成功',
                    'data': model_to_dict_with_url(platform_info),
                })
            else:
                return JsonResponse({
                    'code': 1,
                    'message': '平台信息不存在',
                })
        except Exception as e:
            return JsonResponse({
                'code': 1,
                'message': '获取平台信息失败',
                'data': str(e),
            })
    else:
        return JsonResponse({
            'code': 3,
            'message': '请求方式错误',
        })

# 更新平台信息
def update_platform_info(request):
    if request.method == 'POST':
        try:
            platform_info = PlatformInfo.objects.first()
            if not platform_info:
                return JsonResponse({
                    'code': 1,
                    'message': '平台信息不存在',
                })
            
            form = PlatformInfoForm(request.POST, request.FILES, instance=platform_info)
            if form.is_valid():
                form.save()
                return JsonResponse({
                    'code': 0,
                    'message': '平台信息更新成功',
                    'data': model_to_dict_with_url(form.instance),
                })
            else:
                return JsonResponse({
                    'code': 1,
                    'message': '平台信息更新失败',
                    'data': form.errors,
                })
        except Exception as e:
            return JsonResponse({
                'code': 1,
                'message': '更新平台信息失败',
                'data': str(e),
            })
    else:
        return JsonResponse({
            'code': 3,
            'message': '请求方式错误',
        })

# 删除平台信息
def delete_platform_info(request):
    if request.method == 'POST':
        try:
            PlatformInfoID = request.POST.get('PlatformInfoID')
            if not PlatformInfoID:
                return JsonResponse({
                    'code': 2,
                    'message': '平台信息ID不能为空',
                })
            platform_info = PlatformInfo.objects.filter(platform_id=PlatformInfoID).first()
            if platform_info:
                if platform_info.logo:
                    platform_info.logo.delete()
                platform_info.delete()
                return JsonResponse({
                    'code': 0,
                    'message': '平台信息删除成功',
                })
            else:
                return JsonResponse({
                    'code': 1,
                    'message': '平台信息不存在',
                })
        except Exception as e:
            return JsonResponse({
                'code': 1,
                'message': '删除平台信息失败',
                'data': str(e),
            })
    else:
        return JsonResponse({
            'code': 3,
            'message': '请求方式错误',
        })


# 获取全部平台
def get_all_platform(request):
    if request.method == 'POST':
        try:
            platform_list = PlatformInfo.objects.all()
            return JsonResponse({
                'code': 0,
                'message': '获取全部平台成功',
                'data': [model_to_dict_with_url(platform) for platform in platform_list],
            })
        except Exception as e:
            return JsonResponse({
                'code': 1,
                'message': '获取全部平台失败',
                'data': str(e),
            })
    else:
        return JsonResponse({
            'code': 3,
            'message': '请求方式错误',
        })
